/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-11-24，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.ui.adapter;

import java.util.ArrayList;
import java.util.List;
import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.model.Menu;

import android.R.anim;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuAdapter extends BaseAdapter
{

    private int pressedId = -1;// 选中的菜单
    private Context context;
    private List<Menu> list;// 菜单
    private boolean isPressed[];// 所有菜单的选中状态

    public MenuAdapter(int pressedId, Context context)
    {
        this.context = context;
        this.pressedId = pressedId;
        this.list = new ArrayList<Menu>();
        initMenuData();
    }

    /**
     * 初始化菜单相关数据
     */
    private void initMenuData()
    {
        String[] menuNames = context.getResources().getStringArray(
            R.array.menu_names);
        int[] menuIds = context.getResources().getIntArray(R.array.menu_ids);
        int[] un_sel_images = { R.drawable.ic_send_sms_unsel,
            R.drawable.ic_history_unsel };
        int[] sel_images = { R.drawable.ic_send_sms_sel,
            R.drawable.ic_history_sel };
        for (int i = 0; i < menuNames.length; i++)
        {
            list.add(new Menu(menuIds[i], un_sel_images[i], sel_images[i],
                menuNames[i]));
        }
        // 初始化菜单选中状态
        isPressed = new boolean[list.size()];
        // 将每个菜单选中状态置为false
        for (int i = 0; i < isPressed.length; i++)
        {
            isPressed[i] = false;
        }
        // 设置默认选中的菜单
        isPressed[pressedId] = true;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        MenuHolder holder = null;
        if (convertView == null)
        {
            holder = new MenuHolder();
            convertView = LayoutInflater.from(context).inflate(
                R.layout.menu_item, null);
            holder.tvMenuName = (TextView) convertView
                .findViewById(R.id.tv_menu_name);
            holder.tvMenuId = (TextView) convertView
                .findViewById(R.id.tv_menu_id);
            holder.tvMenuArrow = (ImageView) convertView
                .findViewById(R.id.tv_menu_arrow);
            convertView.setTag(holder);
        }
        else
        {
            holder = (MenuHolder) convertView.getTag();
        }
        holder.tvMenuName.setTextColor(context.getResources().getColor(
            R.color.white));
        Menu menu = list.get(position);
        if (isPressed[position])
        {
            convertView.setBackgroundResource((R.drawable.menu_sel_bg));
            holder.tvMenuArrow.setImageResource(menu.sel_menu_arrow);
        }
        else
        {
            convertView.setBackgroundResource((R.color.menu_un_select_bg));
            holder.tvMenuArrow.setImageResource(menu.un_sel_menu_arrow);
        }
        holder.tvMenuName.setText(menu.menu_name);
        holder.tvMenuId.setText(String.valueOf(menu.menu_id));
        return convertView;
    }

    public void changeState(int position)
    {

        for (int i = 0; i < this.list.size(); i++)
        {
            isPressed[i] = false;
        }
        isPressed[position] = true;
        notifyDataSetInvalidated();
    }

    class MenuHolder
    {
        TextView tvMenuName;
        TextView tvMenuId;
        ImageView tvMenuArrow;
    }
}
